package c_小程序登录.a_小程序登录;

import a_接口调用凭证.a_获取接口调用凭据.GetAccessToken;
import b_openApi管理.b_查询API调用额度.GetQuota;
import com.fasterxml.jackson.annotation.JsonProperty;
import my.wechat.rc.common.HttpClientUtil;
import my.wechat.rc.common.MapUtil;

/**
 * @author 86153
 * @date 2022/7/30
 **/
public class Code2Session {

    public static Code2SessionResponse code2Session(String jsCode) {
        try {
            String url = "https://api.weixin.qq.com/sns/jscode2session";
            return HttpClientUtil.get(url, MapUtil.of("appid", GetAccessToken.appid,
                    "secret", GetAccessToken.secret,
                    "js_code", jsCode,//登录时获取的 code，可通过wx.login获取,请参照add.md
                    "grant_type", "authorization_code"), Code2SessionResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Code2SessionResponse();
    }

    public static Code2SessionResponse code2Session() {
        return code2Session("093PCanl2WJ9B94EFpll27Njm64PCanB");
    }

    public static void main(String[] args) {
        code2Session();
    }

    public static class Code2SessionResponse {
        @JsonProperty("session_key")
        private String sessionKey;//会话密钥
        private String unionid;//用户在开放平台的唯一标识符，若当前小程序已绑定到微信开放平台帐号下会返回，详见 UnionID 机制说明。
        private String errmsg;//错误信息
        private String openid;//用户唯一标识
        private Integer errcode;//错误码

        public String getSessionKey() {
            return sessionKey;
        }

        public void setSessionKey(String sessionKey) {
            this.sessionKey = sessionKey;
        }

        public String getUnionid() {
            return unionid;
        }

        public void setUnionid(String unionid) {
            this.unionid = unionid;
        }

        public String getErrmsg() {
            return errmsg;
        }

        public void setErrmsg(String errmsg) {
            this.errmsg = errmsg;
        }

        public String getOpenid() {
            return openid;
        }

        public void setOpenid(String openid) {
            this.openid = openid;
        }

        public Integer getErrcode() {
            return errcode;
        }

        public void setErrcode(Integer errcode) {
            this.errcode = errcode;
        }
    }

}
